<include file="common:__head__"/>
<div class="permissions-tabs">
    <a class="role tabs-bg" href="{:U('Agent/index')}">代理列表</a>
</div>
<div class="permissions-content">
    <form class="form-horizontal" id="to_agent" action="{:U('Agent/add_agent')}" method="post">
        <div class="permissions-le">
            <span class="add-admin"><a href="javascript:history.go(-1)"></a>代理添加操作</span>
            <div class="message-set">
                <span class="hints">操作提示</span>
                <span class="hints-dec">系统平台全局设置,包括基础设置、购物、短信、邮件、水印和分销等相关模块。</span>
            </div>
            <div class="add-admin-ipt">
                <span><i>*</i>代理名称:</span>
                <input type="text" id="c_name" name="c_name" placeholder="请填写代理姓名" class="w200">
            </div>
            <div class="add-admin-ipt">
                <span><i>*</i>手机号:</span>
                <input type="text" name="mobile" id="mobile" data-type="number" maxlength="11" onchange="checkIsset(this.value);" placeholder="请填写代理手机号" class="w200"/>
                <span id="err_mobile" style="margin-left:10px;"></span>
            </div>
            <div class="add-admin-ipt">
                <span><i>*</i>上级代理:</span>
                <input type="text" id="search_to">
                <select name="to_id" id="pid" style="margin-left:5px;">
                    <option value="0">顶级代理</option>
                </select>
            </div>
            <div class="add-admin-ipt">
                <span></span>
                <a class="btn-lg btn-blue" id="submitBtn">保存</a>
            </div>
        </div>
    </form>
    <div class="permissions-ri">
        <a href="" class="permissions-help">帮助和服务</a>
    </div>
</div>
<script type="text/javascript">

    $(function(){
        $('#submitBtn').click(function () {
            var mobile = $('#mobile').val();
            var c_name = $('#c_name').val();
            if(!c_name){
                layer.alert('请输入代理名称');
                return false;
            }
            var mobilePattern = /^0?1[3|4|5|8|7][0-9]\d{8}$/;
            if (mobile == '' || !mobilePattern.test(mobile)) {
                layer.alert('请输入正确的手机号码');
                return false;
            }
            $("#to_agent").submit();
        });

        $("#search_to").keyup(function(){
            $aName = $(this).val();
            if($aName != ''){
                getAgentName();
            }else{
                return false;
            }
        });

        $("#search_to").change(function(){
            $aName = $(this).val();
            if($aName != ''){
                getAgentName();
            }else{
                return false;
            }
        });

        function getAgentName()
        {
            $.ajax({
                url: "{:U('Agent/searchAgent')}",
                type: "post",
                data: {aName : $aName},
                success: function (data)
                {
                    options = '<option value="0">顶级代理</option>';
                    if (data) {
                        $.each(data, function(k, v){
                            options += '<option value="'+ v.agent_id +'">'+ v.agent_name +'  '+v.mobile+'</option>';
                        });
                    }
                    $("#pid").html(options);
                }
            });
        }
    });
    function checkIsset( $searchKey)
    {
        $.ajax({
            url: "{:U('Agent/searchIsset')}",
            type: "post",
            data: {skey : $searchKey},
            dataType:'JSON',
            success: function (msg)
            {
                if(msg.status == 'success'){
                    $("#err_mobile").show();
                    $("#err_mobile").css("color","green");
                    $("#err_mobile").text("手机号码可用");
                }else{
                    $("#err_mobile").show();
                    $("#err_mobile").css("color","red");
                    $("#err_mobile").text(msg.msg);
                }
            }
        });
    }
</script>
<include file="common:__foot__"/>